<?php

namespace App\Exports;

use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;

class MemberdrawExport implements FromQuery, WithMapping, WithHeadings
{
    use Exportable;

    protected  $model;

    public function __construct($model)
    {
        $this->model = $model;
    }

    /**
    * @return \Illuminate\Support\Collection
    */
    public function query()
    {
        return $this->model->query()->where(function($query){
            $query->with('member');
            $query->where('member_id',request()->id);
            if(request()->type)
            {
                $query->where('type',request()->type);
            }
            if(isset(request()->time[0]) && !empty(request()->time[0])){
                $query->whereBetween('created_at', request()->time);
            }
        });
    }

    public function map($invoice): array
    {
        return [
            $invoice->type,
            $invoice->money,
            $invoice->desc,
            $invoice->money_type == 1 ? '金豆' : $invoice->money_type == 2 ? '开心果' : '金额'
        ];
    }

    public function headings(): array
    {
        return [
            '交易类型',
            '金豆数量',
            '描述',
            '收益类型',
            '交易时间',
        ];
    }
}
